package com.ponchware.classes
{
	import flash.events.Event;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.events.EffectEvent;

	[Event(name="complete", type="flash.events.Event")]
	public class Explosion extends UIComponent
	{
		private const MAX_PARTICLES:Number = 100;
		private const MAX_RADIUS:Number = 10;
		private const MAX_SPEED:Number = 15;
		
		private var particles:ArrayCollection;
		private var gameObjects:Array;
		
		private var fadeOut:Fade;
		
		public function Explosion(ex:Number, ey:Number, gameObjects:Array)
		{
			super();

			this.x = ex;
			this.y = ey;
			this.gameObjects = gameObjects;
			
			particles = new ArrayCollection();
			
			fadeOut = new Fade();
			fadeOut.alphaFrom = 1;
			fadeOut.alphaTo = 0;
			fadeOut.duration = 2000;
		}
		
		protected override function createChildren():void
		{
			super.createChildren();
			for (var i:Number = 0; i < MAX_PARTICLES; i++)
			{
				// Calculate an angle from 0-180 degrees
				
				var angle:Number = Math.random() * Math.PI;
				trace("Angle: " + angle, "Degrees: " + angle * 180/Math.PI, "cos(angle): " + Math.cos(angle), "sin(angle): " + Math.sin(angle));
				var r:Number = MAX_RADIUS; //Math.random() * MAX_RADIUS;
				var s:Number = Math.random() * MAX_SPEED;
				var px:Number = Math.cos(angle) * r;
				var py:Number = 0 - Math.sin(angle) * r;
				
				//trace("x: ", px, "y ", py);
				
				var p:Particle = new Particle(px, py, angle, s);
				
				particles.addItem(p);
				//p.addEventListener(Event.ENTER_FRAME, onEnterFrame);
				this.addChild(p);
			}
			
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		
		private function onEnterFrame(e:Event):void
		{
			/* var p:Particle = e.target as Particle;
			
			if (!p.updatePosition())
			{
				p.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				this.removeChild(p);
				p = null;
			} */
			if (particles.length > 0)
			{
				var p:Particle;
				var iterator:IViewCursor = particles.createCursor();
				
				while (!iterator.afterLast)
				{
					p = Particle(iterator.current);
					p.updatePosition()
					
					var point:Point = new Point(p.width/2, p.height);
		
					point = p.localToGlobal(point);
					var hit:Boolean = false;
					var i:Number = 0;
					while (!hit && i < gameObjects.length)
					{
						hit = gameObjects[i].hitTestPoint(point.x,point.y, true);
						i++;
					}
					
					var pos:Point = new Point(p.x, p.y);
					pos = this.localToGlobal(pos);
					
					//if (hit || p.x < 0 || p.x > this.parentApplication.width || p.y > this.parentApplication.height)
					if (hit || pos.x < 0 || pos.x > this.parentApplication.width || pos.y > this.parentApplication.height)
					//if (hit)
					{
						iterator.remove();
						var d:Date =  new Date();
						trace(d.toLocaleTimeString() + " Removed one particle");
						/* point = this.globalToLocal(point);
						this.graphics.beginFill(0, 1);
						this.graphics.drawRect(p.x, p.y, p.width, p.height);
						this.graphics.endFill(); */
					}
					else
					{
						// Move the cursor to the next item
						iterator.moveNext();
					}
				}
			}
			else
			{
				// The explosion is done, fade out
				this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				fadeOut.addEventListener(EffectEvent.EFFECT_END, cleanup);
				fadeOut.play([this]);
			}
			
		}
		
		// Once we've faded out cleanup
		private function cleanup(e:EffectEvent):void
		{
			this.parentDocument.removeChild(this);
		}
		
	}
}